﻿/**************************************
* Copyright by Spring Studio(http://springstudio.cn)
* 编码规则定制界面js脚本，及编码创建引擎
***************************************/

/*
* 窗体载入时定位焦点
*/
document.body.onload = function() {
    document.body.focus();
}

/*
* 编码项类型
*/
document.getElementById("type").onchange = function() {
    if (document.getElementById("type").value != "") {
        document.getElementById("singlecate").style.display = "none";
        document.getElementById("info").style.display = "none";
        document.getElementById("infoserial").style.display = "none";
        document.getElementById("date").style.display = "none";
        document.getElementById("text").style.display = "none";
        document.getElementById("serial").style.display = "none";
        document.getElementById("multicate").style.display = "none";
        document.getElementById(document.getElementById("type").value).style.display = "block";
        document.getElementById("note_save").style.display = "block";
    }
    else {
        document.getElementById("singlecate").style.display = "none";
        document.getElementById("info").style.display = "none";
        document.getElementById("infoserial").style.display = "none";
        document.getElementById("date").style.display = "none";
        document.getElementById("text").style.display = "none";
        document.getElementById("serial").style.display = "none";
        document.getElementById("multicate").style.display = "none";
        document.getElementById("note_save").style.display = "none";
    }
    document.getElementById("type").blur();
}

/*
* 文本类型
*/
document.getElementById("text_source1").onclick = function() {
    document.getElementById("span_preset").style.display = "block";
    document.getElementById("span_manual").style.display = "none";
}

document.getElementById("text_source2").onclick = function() {
    document.getElementById("span_preset").style.display = "none";
    document.getElementById("span_manual").style.display = "block";
}

/*
* 日期类型
*/
document.getElementById("date_year").onclick = function() {
    document.getElementById("year").style.display = "block";
    document.getElementById("month").style.display = "none";
    document.getElementById("day").style.display = "none";
}

document.getElementById("date_month").onclick = function() {
    document.getElementById("year").style.display = "none";
    document.getElementById("month").style.display = "block";
    document.getElementById("day").style.display = "none";
}

document.getElementById("date_day").onclick = function() {
    document.getElementById("year").style.display = "none";
    document.getElementById("month").style.display = "none";
    document.getElementById("day").style.display = "block";
}

/*
* 是否控制索引长度
*/
document.getElementById("check_limit_length").onclick = function() {
    if (document.getElementById("check_limit_length").checked) {
        document.getElementById("limit_length").style.display = "block";
    }
    else {
        document.getElementById("limit_length").style.display = "none";
    }
}

/*
* 是否限定索引层次
*/
document.getElementById("check_limit_level").onclick = function() {
    if (document.getElementById("check_limit_level").checked) {
        document.getElementById("singlecate_level").style.visibility = "visible";
        document.getElementById("singlecate_level").focus();
    }
    else {
        document.getElementById("singlecate_level").style.visibility = "hidden";
    }
}

/**
* 弹出索引选择框
*/
function OpenCate() {
    var result = window.showModalDialog("../CodeIndex/CateSelector.aspx", window, "dialogWidth:350px;dialogHeight:520px");
    if (result != undefined) {
        document.getElementById("singlecate_source").value = result;
    }
}

/*
* 点击确定按钮，获取规则模型XML
*/
function GetModuleXML() {
    // 验证编码规则中是否只含有一个流水号
    var serial = 0;
    var regions = infocode.childNodes;
    for (var i = 0; i < regions.length; i++) {
        if (regions[i].getAttribute("type") == "serial") {
            serial++;
        }
    }

    if (serial > 1) {
        alert("规则表达式错误：只允许有一个顺序流水号类型的编码项！");
        return;
    }
    
    window.dialogArguments.document.getElementById("RuleModule").value = moduleXml.xml;
    self.close();
}

/**
* 准备好XML对象
*/
var moduleXml = null;
var infocode = null;

if (navigator.userAgent.indexOf("Firefox") > 0) {
    moduleXml = document.implementation.createDocument("text/xml", "", null);
}
else if (navigator.userAgent.indexOf("MSIE") > 0) {
    moduleXml = new ActiveXObject("Microsoft.XMLDOM");
    moduleXml.async = false;
}

if (window.dialogArguments.document.getElementById("RuleModule").value.trim() == "") {
    moduleXml.loadXML("");
    infocode = moduleXml.createElement("infocode");
    moduleXml.appendChild(infocode);
}
else {
    moduleXml.loadXML(window.dialogArguments.document.getElementById("RuleModule").value);
    infocode = moduleXml.getElementsByTagName("infocode")[0];
}

var region = null;

/*
* 添加编码节到编码表达式
*/
function AddRegion() {
    if (document.getElementById("type").value != "")
    {
        // 创建节点
        region = moduleXml.createElement("region");
        region.setAttribute("type", document.getElementById("type").value);  // 类型属性

        // 是否必填项
        if (document.getElementById("request1").checked) {
            region.setAttribute("request", document.getElementById("request1").value);
        }
        else if (document.getElementById("request2").checked) {
            region.setAttribute("request", document.getElementById("request2").value);
        }
        
        // 是否不参与生成流水号
        if (document.getElementById("unserial").checked) {
            region.setAttribute("unserial", document.getElementById("unserial").value);
        }
        
        switch (document.getElementById("type").value) {
            case "singlecate":
                document.getElementById("singlecate_source").value = document.getElementById("singlecate_source").value.trim();
                if (document.getElementById("singlecate_source").value == "") {
                    alert("索引不能为空！");
                    return false;
                }
                if (document.getElementById("check_limit_length").checked) {
                    if (!checkNaturalNumber(document.getElementById("singlecate_length").value)) {
                        alert("索引长度必须必须是正整数！");
                        document.getElementById("singlecate_length").focus();
                        document.getElementById("singlecate_length").select();
                        return false;
                    }

                    if (document.getElementById("singlecate_supplement").value == "") {
                        alert("补齐文本不能为空！");
                        document.getElementById("singlecate_supplement").focus();
                        return false;
                    }
                    region.setAttribute("length", document.getElementById("singlecate_length").value);
                    region.setAttribute("supplement", document.getElementById("singlecate_supplement").value);
                    if (document.getElementById("singlecate_backward").checked) {
                        region.setAttribute("position", document.getElementById("singlecate_backward").value);
                    }
                    else if (document.getElementById("singlecate_forward").checked) {
                        region.setAttribute("position", document.getElementById("singlecate_forward").value);
                    }
                }
                if (document.getElementById("check_limit_level").checked) {
                    if (!checkNaturalNumber(document.getElementById("singlecate_level").value)) {
                        alert("索引限定层次必须必须是正整数！");
                        document.getElementById("singlecate_level").focus();
                        document.getElementById("singlecate_level").select();
                        return false;
                    }
                    region.setAttribute("level", document.getElementById("singlecate_level").value);
                }
                region.setAttribute("source", document.getElementById("singlecate_source").value);
                document.getElementById("singlecate_source").value = "";
                document.getElementById("check_limit_length").checked = false;
                document.getElementById("limit_length").style.display = "none";
                document.getElementById("check_limit_level").checked = false;
                document.getElementById("singlecate_level").value = "";
                document.getElementById("singlecate_level").style.visibility = "hidden";
                document.getElementById("singlecate_length").value = "";
                document.getElementById("singlecate_supplement").value = "";
                document.getElementById("singlecate_backward").checked = true;
                break;
            case "info":
                document.getElementById("info_source").value = document.getElementById("info_source").value.trim();
                if (document.getElementById("info_source").value == "") {
                    alert("请选择编码类型！");
                    return false;
                }
                region.setAttribute("source", document.getElementById("info_source").value);
                document.getElementById("info_source").value = "";
                break;
            case "infoserial":
                document.getElementById("infoserial_source").value = document.getElementById("infoserial_source").value.trim();
                if (document.getElementById("infoserial_source").value == "") {
                    alert("请选择编码类型！");
                    return false;
                }
                region.setAttribute("source", document.getElementById("infoserial_source").value);
                document.getElementById("infoserial_source").value = "";
                break;
            case "date":
                if (document.getElementById("date_year").checked) {
                    region.setAttribute("type", "year");
                    if (document.getElementById("year_current").checked) {
                        region.setAttribute("current", "1");
                    }
                    else {
                        region.setAttribute("current", "0");
                    }
                }
                else if (document.getElementById("date_month").checked) {
                    region.setAttribute("type", "month");
                    if (document.getElementById("month_current").checked) {
                        region.setAttribute("current", "1");
                    }
                    else {
                        region.setAttribute("current", "0");
                    }
                }
                else if (document.getElementById("date_day").checked) {
                    region.setAttribute("type", "day");
                    if (document.getElementById("day_current").checked) {
                        region.setAttribute("current", "1");
                    }
                    else {
                        region.setAttribute("current", "0");
                    }
                }

                document.getElementById("year_current").checked = false;
                document.getElementById("month_current").checked = false;
                document.getElementById("day_current").checked = false;
                document.getElementById("date_year").click();
                break;
            case "text":
                if (document.getElementById("text_source1").checked) {
                    region.setAttribute("source", document.getElementById("text_source1").value);

                    document.getElementById("text_text").value = document.getElementById("text_text").value.trim();
                    if (document.getElementById("text_text").value == "") {
                        alert("预置文本不能为空！");
                        document.getElementById("text_text").focus();
                        return false;
                    }
                    region.setAttribute("text", document.getElementById("text_text").value);
                }
                else if (document.getElementById("text_source2").checked) {
                    region.setAttribute("source", document.getElementById("text_source2").value);

                    document.getElementById("text_length").value = document.getElementById("text_length").value.trim();
                    if (document.getElementById("text_length").value == "") {
                        alert("请设置文本长度！");
                        document.getElementById("text_length").focus();
                        return false;
                    }
                    else if (!checkNonnegative(document.getElementById("text_length").value)) {
                        alert("文本长度必须在0~9之间！");
                        document.getElementById("text_length").focus();
                        document.getElementById("text_length").select();
                        return false;
                    }
                    else if (document.getElementById("text_default").value != "" && document.getElementById("text_length").value != "0" && document.getElementById("text_default").value.length != document.getElementById("text_length").value) {
                        alert("默认值文本长度必须等于设置的文本长度！");
                        document.getElementById("text_default").focus();
                        document.getElementById("text_default").select();
                        return false;
                    }
                    region.setAttribute("length", document.getElementById("text_length").value);
                    region.setAttribute("default", document.getElementById("text_default").value);
                }
                
                // 重置各输入项
                document.getElementById("text_text").value = "";
                document.getElementById("text_length").value = "";
                document.getElementById("text_default").value = "";
                document.getElementById("text_source1").click();
                break;
            case "serial":
                document.getElementById("serial_length").value = document.getElementById("serial_length").value.trim();
                if (document.getElementById("serial_length").value == "") {
                    alert("请输入编号长度！");
                    document.getElementById("serial_length").focus();
                    return false;
                }
                else if (!checkNonnegative(document.getElementById("serial_length").value)) {
                    alert("编号长度必须在0~9之间！");
                    document.getElementById("serial_length").focus();
                    document.getElementById("serial_length").select();
                    return false;
                }
                if (document.getElementById("serial_start").value != "" && !checkNaturalNumber(document.getElementById("serial_start").value)) {
                    alert("起始编号必须是正整数！");
                    document.getElementById("serial_start").focus();
                    document.getElementById("serial_start").select();
                    return false;
                }
                if (document.getElementById("serial_start").value != "" && document.getElementById("serial_length").value != "0" && document.getElementById("serial_start").value.length != document.getElementById("serial_length").value) {
                    alert("起始编号必须符合编号长度定义！");
                    document.getElementById("serial_start").focus();
                    document.getElementById("serial_start").select();
                    return false;
                }
                region.setAttribute("length", document.getElementById("serial_length").value);
                region.setAttribute("start", document.getElementById("serial_start").value);
                document.getElementById("serial_length").value = "";
                document.getElementById("serial_start").value = "";

                // 是否显示已有编码
                if (document.getElementById("serial_exist").checked) {
                    region.setAttribute("exist", "1");
                }
                document.getElementById("serial_exist").checked = false;

                break;
            case "multicate":
                document.getElementById("multicate_source").value = document.getElementById("multicate_source").value.trim();
                if (document.getElementById("multicate_source").value == "") {
                    alert("索引不能为空！");
                    return false;
                }
                region.setAttribute("source", document.getElementById("multicate_source").value);
                region.setAttribute("separator", document.getElementById("multicate_separator").value);
                document.getElementById("multicate_source").value = "";
                document.getElementById("multicate_separator").value = "";
                break;
        }

        region.text = document.getElementById("note").value;
        infocode.appendChild(region);  // 节点添加到XML
        
        document.getElementById("type").value = "";
        document.getElementById("note").value = "";
        
        document.getElementById("singlecate").style.display = "none";
        document.getElementById("info").style.display = "none";
        document.getElementById("infoserial").style.display = "none";
        document.getElementById("date").style.display = "none";
        document.getElementById("text").style.display = "none";
        document.getElementById("serial").style.display = "none";
        document.getElementById("multicate").style.display = "none";
        document.getElementById("note_save").style.display = "none";

        document.getElementById("request1").checked = true;
        document.getElementById("unserial").checked = false;
    }
    
    ParseFormula();  // 刷新表达式
}

/*
* 解析并显示规则表达式
*/
function ParseFormula() {
    var formula = "";
    var regions = infocode.childNodes;
    for (var i = 0; i < regions.length; i++) {
        var color = "";
        var text = "";
        var tips = "";
        tips += "-<font color=#008000>中文说明：</font>" + regions[i].text;
        if (regions[i].getAttribute("unserial") != null && regions[i].getAttribute("unserial") == "1") {
            tips += "<font color=#808080>(不影响流水号的生成)</font>";
        }
        tips += "<br>";
        tips += "-<font color=#008000>" + ParseRequest(regions[i].getAttribute("request")) + "：</font>";
        switch (regions[i].getAttribute("type")) {
            case "singlecate":
                color = "#ffcfcf";
                text = "NN";
                tips += "单索引字典值<br>";
                tips += "-<font color=#008000>数据来源：</font><span style=&quot;cursor:pointer;&quot; onclick=javascript:GetCateName(&quot;" + regions[i].getAttribute("source") + "&quot;," + i + ");>" + regions[i].getAttribute("source") + "</span><span id=&quot;CateName_" + i + "&quot;></span><br>";
                if (regions[i].getAttribute("length") != null && regions[i].getAttribute("length") != "") {
                    text = RepeatString("N", regions[i].getAttribute("length"));
                    tips += "-<font color=#008000>索引长度：</font>" + regions[i].getAttribute("length");
                    tips += " <font color=#008000>补齐文本：</font>" + regions[i].getAttribute("supplement");
                    tips += " <font color=#008000>[" + ParsePosition(regions[i].getAttribute("position")) + "]</font><br>";
                }
                if (regions[i].getAttribute("level") != null && regions[i].getAttribute("level") != "") {
                    tips += "-<font color=#008000>限定索引层次：</font>" + regions[i].getAttribute("level") + "<br>";
                }
                break;
            case "info":
                color = "#cfffcf";
                text = "OO";
                tips += "已有编码数据<br>";
                tips += "-<font color=#008000>数据来源：</font><span style=&quot;cursor:pointer;&quot; onclick=javascript:GetRuleName(&quot;" + regions[i].getAttribute("source") + "&quot;," + i + ");>" + regions[i].getAttribute("source") + "</span><span id=&quot;RuleName_" + i + "&quot;></span><br>";
                break;
            case "infoserial":
                color = "#efefef";
                text = "SS";
                tips += "已有流水号<br>";
                tips += "-<font color=#008000>数据来源：</font><span style=&quot;cursor:pointer;&quot; onclick=javascript:GetRuleName(&quot;" + regions[i].getAttribute("source") + "&quot;," + i + ");>" + regions[i].getAttribute("source") + "</span><span id=&quot;RuleName_" + i + "&quot;></span><br>";
                break;
            case "year":
                color = "#cfcfff";
                text = "YYYY";
                tips += "四位数年份<br>";
                if (regions[i].getAttribute("current") == "1") {
                    tips += "-<font color=#008000>数据来源：</font>当前年份<br>";
                }
                else if (regions[i].getAttribute("current") == "0") {
                    tips += "-<font color=#008000>数据来源：</font>不限年份<br>";
                }
                break;
            case "month":
                color = "#cfcfff";
                text = "MM";
                tips += "二位数月份<br>";
                if (regions[i].getAttribute("current") == "1") {
                    tips += "-<font color=#008000>数据来源：</font>当前月份<br>";
                }
                else if (regions[i].getAttribute("current") == "0") {
                    tips += "-<font color=#008000>数据来源：</font>不限月份<br>";
                }
                break;
            case "day":
                color = "#cfcfff";
                text = "DD";
                tips += "二位数日期<br>";
                if (regions[i].getAttribute("current") == "1") {
                    tips += "-<font color=#008000>数据来源：</font>当前日期<br>";
                }
                else if (regions[i].getAttribute("current") == "0") {
                    tips += "-<font color=#008000>数据来源：</font>不限日期<br>";
                }
                break;
            case "text":
                color = "#cfffff";
                tips += "普通文本<br>";
                if (regions[i].getAttribute("source") == "preset") {
                    text = regions[i].getAttribute("text");
                    tips += "-<font color=#008000>数据来源：</font>预置文本<br>";
                }
                else if (regions[i].getAttribute("source") == "manual") {
                    if (regions[i].getAttribute("length") == "0") {
                        text = "T~";
                        tips += "-<font color=#008000>数据来源：</font>手动输入，不限长度";
                        if (regions[i].getAttribute("default") != "") {
                            tips += " <font color=#008000>默认值：</font>" + regions[i].getAttribute("default");
                        }
                        tips += "<br>";
                    }
                    else {
                        text = RepeatString("T", regions[i].getAttribute("length"));
                        tips += "-<font color=#008000>数据来源：</font>手动输入，限定长度";
                        if (regions[i].getAttribute("default") != "") {
                            tips += " <font color=#008000>默认值：</font>" + regions[i].getAttribute("default");
                        }
                        tips += "<br>";
                    }
                }
                break;
            case "serial":
                color = "#ffcfff";
                tips += "顺序流水号<br>";
                if (regions[i].getAttribute("length") == "0") {
                    text = "~X";
                    tips += "-<font color=#008000>编号类型：</font>不等长流水号<br>";
                }
                else {
                    text = RepeatString("X", regions[i].getAttribute("length"));
                    tips += "-<font color=#008000>编号类型：</font>定长流水号<br>";
                }

                if (regions[i].getAttribute("start") != "") {
                    tips += "-<font color=#008000>起始编号：</font>" + regions[i].getAttribute("start");
                }

                if (regions[i].getAttribute("exist") != null && regions[i].getAttribute("exist") == "1") {
                    tips += "<font color=#808080>[显示已有编号]</font>";
                }
                
                tips += "<br>";
                break;
            case "multicate":
                color = "#ffffcf";
                tips += "多索引字典值<br>";
                var cates = regions[i].getAttribute("source").split(",");
                for (var n = 0; n < cates.length; n++) {
                    text += "NN" + regions[i].getAttribute("separator");
                }
                text = text.substring(0, text.length - regions[i].getAttribute("separator").length);
                tips += "-<font color=#008000>数据来源：</font><span style=&quot;cursor:pointer;&quot; onclick=javascript:GetCateName(&quot;" + regions[i].getAttribute("source") + "&quot;," + i + ");>" + regions[i].getAttribute("source") + "</span><span id=&quot;CateName_" + i + "&quot;></span><br>";
                break;
        }
        tips += "-[<a href=javascript:DeleteRegion(" + i + ");>删除此编码项</a>]";

        formula += " <span style=\"font-weight: bold; cursor: pointer; border: solid 1px #cfcfcf; background-color: " + color + ";\" onclick=\"javascript:FixTips('" + tips + "');\" onmouseover=\"javascript:ShowTips('" + tips + "');\" onmouseout=\"javascript:if(moveable){HiddenTips();}\" onmousemove=\"javascript:MoveTips();\">" + text + "</span> ";
    }

    document.getElementById("formula").innerHTML = formula;
}

ParseFormula();  // 页面载入时显示表达式

/*
* 显示重复字符串
*/
function RepeatString(str, length) {
    var rtn = "";
    for (var i = 0; i < length; i++) {
        rtn += str;
    }
    return rtn;
}

/*
* 解析必填项与选填项
*/
function ParseRequest(request) {
    if (request == "optional") {
        return "选填项";
    }
    else if (request == "needed") {
        return "必填项";
    }
    else {
        return "";
    }
}

/*
* 解析后补齐与前补齐
*/
function ParsePosition(position) {
    if (position == "backward") {
        return "后补齐";
    }
    else if (position == "forward") {
        return "前补齐";
    }
    else {
        return "";
    }
}

/*
* 删除编码节
*/
function DeleteRegion(n) {
    var regions = infocode.childNodes;
    infocode.removeChild(regions[n]);
    HiddenTips();
    ParseFormula();
}

/*
* 利用Ajax获取索引数据来源的中文描述
*/
function GetCateName(cateGuit, indexCateName) {
    objCateName = document.getElementById("CateName_" + indexCateName);
    if (objCateName != null && objCateName != undefined && objCateName.innerHTML == "") {
        var xmlhttp = GetXmlHttp();
        if (xmlhttp != null) {
            // 定义回调函数
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {
                        objCateName = document.getElementById("CateName_" + indexCateName);
                        if (objCateName != null && objCateName != undefined) {
                            objCateName.innerHTML += "<br>-<font color=#008000>索引名称：</font>" + xmlhttp.responseText;
                        }
                    }
                }
            }
            
            xmlhttp.open("GET", "../CodeIndex/GetCateName.aspx?CateGuid=" + cateGuit, true);
            xmlhttp.send(null);
        }
    }
}

/*
* 利用Ajax获取编码数据来源的中文描述
*/
function GetRuleName(ruleGuit, indexRuleName) {
    objRuleName = document.getElementById("RuleName_" + indexRuleName);
    if (objRuleName != null && objRuleName != undefined && objRuleName.innerHTML == "") {
        var xmlhttp = GetXmlHttp();
        if (xmlhttp != null) {
            // 定义回调函数
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {
                        objRuleName = document.getElementById("RuleName_" + indexRuleName);
                        if (objRuleName != null && objRuleName != undefined) {
                            objRuleName.innerHTML += "<br>-<font color=#008000>编码类型：</font>" + xmlhttp.responseText;
                        }
                    }
                }
            }
            
            xmlhttp.open("GET", "GetRuleName.aspx?RuleGuid=" + ruleGuit, true);
            xmlhttp.send(null);
        }
    }
}